package com.resou.reader.signin.style1;

import android.text.TextUtils;
import android.util.Log;

import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.data.Injection;
import com.resou.reader.data.signin.SignInRepository;
import com.resou.reader.data.signin.model.SignIn;
import com.resou.reader.mine.IView.UserInstance;
import com.resou.reader.signin.SignInView;
import com.resou.reader.utils.ToastUtil;
import com.resou.reader.utils.UserUtil;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class SignInPresenter extends ResouBasePresenter<SignInView> {

    private SignInRepository mRepository;

    public SignInPresenter(SignInView view) {
        super(view);
        mRepository = Injection.provideSignInRepository();
    }

    private static final String TAG = "SignInPresenter";

    public void loadSignInfo() {
        addCompositeDisposable(
                mRepository.loadSignInfo(UserInstance.getToken())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(result -> {
                                    if (result.getCode() == 0) {
                                        mView.showData(result.getData());
                                        mView.showContent();
                                    }
                                }, throwable -> {
                                    ToastUtil.makeLongToast(throwable.getMessage());
                                    mView.showError(throwable);
                                }
                        )
        );
    }

    public void signIn(int weekDay) {
        addCompositeDisposable(
                mRepository.signIn(UserUtil.getToken(), weekDay)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(result -> {
                            if (result.getCode() == 0) {
                                SignIn data = result.getData();
                                mView.onSignInSucceed(data);
                                //签到成功需要刷新列表
                                loadSignInfo();
                                mView.showContent();
                            } else {
                                Log.d(TAG, "签到失败：" + result.getMsg());
                            }
                        }, throwable -> {
                            mView.onSignInFailed(throwable);
                            mView.showError(throwable);
                            Log.d(TAG, "签到错误:" + throwable.getMessage());
                        })
        );
    }

    /**
     * 获取绑定任务
     */
//    public void loadBindTask() {
//        addCompositeDisposable(
//                mRepository.getBindTask()
//                        .subscribeOn(Schedulers.io())
//                        .observeOn(AndroidSchedulers.mainThread())
//
//
//        );
//    }

    /**
     * 领取任务奖励
     */
    public void receiveWelfare(String taskId) {
        String token = UserUtil.getToken();
        if (TextUtils.isEmpty(token)) {
            ToastUtil.makeShortToast("您尚未登录，请先登录后重试");
            return;
        }
        Log.d(TAG, "loadBingTask: " + token);
        addCompositeDisposable(
                mRepository.receiveWelfare(token, taskId)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(result -> {
                            if (result.getCode() == 0) {
                                mView.setReceivingCompleted();
                            }
                        }, throwable -> mView.showError(throwable))
        );
    }
}
